﻿using System;
using System.Collections.Generic;
using System.Collections;
using UnityEngine;

namespace XHFrame
{
    /// <summary>
    /// 索引消息类
    /// </summary>
    public class Message : IEnumerable<KeyValuePair<string, object>>
    {
        private Dictionary<string, object> dicDatas = null;

        public string Name { get; private set; }
        public object Sender { get; private set; }
        public object Content { get; set; }

        #region 提供索引器方法实现 Message[Key] = value or data = message[key]
        /// <summary>
        /// 索引器方法
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object this[string key]
        {
            get
            {
                if (null == dicDatas || !dicDatas.ContainsKey(key))
                    return null;
                return dicDatas[key];
            }
            set
            {
                if (null == dicDatas)
                    dicDatas = new Dictionary<string, object>();
                if (dicDatas.ContainsKey(key))
                    dicDatas[key] = value;
                else
                    dicDatas.Add(key, value);
            }
        }
        #endregion

        #region IEnumerator Implementaion (IEnumerator 接口实现)

        /// <summary>
        /// IEnumerator 接口实现
        /// </summary>
        /// <returns></returns>
        public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
        {
            if (null == dicDatas)
                yield break;
            foreach (KeyValuePair<string, object> kvp in dicDatas)
            {
                yield return kvp;
            }
        }

        #endregion



        IEnumerator IEnumerable.GetEnumerator()
        {
            return dicDatas.GetEnumerator();
        }

        #region 消息 构造函数
        /// <summary>
        /// 消息构造
        /// </summary>
        /// <param name="name">消息名(类似定义List的变量名)</param>
        /// <param name="sender">发送者</param>
        public Message(string name, object sender)
        {
            Name = name;
            Sender = sender;
            Content = null;
        }
        /// <summary>
        /// 消息构造
        /// </summary>
        /// <param name="name">消息名(类似定义List的变量名)</param>
        /// <param name="sender">发送者</param>
        /// <param name="content">内容</param>
        public Message(string name, object sender, object content)
        {
            Name = name;
            Sender = sender;
            Content = content;
        }
        /// <summary>
        /// 消息构造
        /// </summary>
        /// <param name="name">消息名(类似定义List的变量名)</param>
        /// <param name="sender">发送者</param>
        /// <param name="content">内容</param>
        /// <param name="_dicParams">可变参数</param>
        public Message(string name, object sender, object content, params object[] _dicParams)
        {
            Name = name;
            Sender = sender;
            Content = content;
            if (_dicParams.GetType() == typeof(Dictionary<string, object>))
            {
                foreach (object _dicParam in _dicParams)
                {
                    foreach (KeyValuePair<string, object> kvp in _dicParam as Dictionary<string, object>)
                    {
                        this[kvp.Key] = kvp.Value;
                    }
                }
            }
        }
        /// <summary>
        /// 消息构造
        /// </summary>
        /// <param name="message">消息体(封装好的完整消息信息类)</param>
        public Message(Message message)
        {
            Name = message.Name;
            Sender = message.Sender;
            Content = message.Content;

            foreach (KeyValuePair<string, object> kvp in message.dicDatas)
            {
                dicDatas[kvp.Key] = kvp.Value;
            }

        }
        #endregion

        #region Add & Remove 添加与删除
        /// <summary>
        /// 添加消息信息
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(string key, object value)
        {
            this[key] = value;
        }

        /// <summary>
        /// 删除消息信息
        /// </summary>
        /// <param name="key"></param>
        public void Remove(string key)
        {
            if (dicDatas != null && dicDatas.ContainsKey(key))
            {
                dicDatas.Remove(key);
            }
        }

        #endregion

        #region Send 发送
        /// <summary>
        /// 发送消息信息
        /// </summary>
        public void Send()
        {
            MessageCenter.Instance.SendMessage(this);
        }
        #endregion
    }
}
